var xlsx = require("node-xlsx");
var fs = require('fs');
var file_util = require('./public/file_util');
var path = require('path');
var ExportExcel = require('./src/ExportExcel');
const ExportLang = require("./src/ExportLang");
const TSExport = require('./src/TSExport')
const CSExport = require('./src/CSExport')
const Define = require('./src/ExportDefine')

// var cur_path = process.cwd()

var ignoreFileList = ['$', '.~', '~', '.json', '.bat']
var excelList = []
var exports = []
exports.push(new TSExport())
exports.push(new CSExport())
var configObj = null;
function parseFile(fileName, exportPath) {

	for (let index = 0; index < ignoreFileList.length; index++) {
		const element = ignoreFileList[index];
		if (fileName.indexOf(element) >= 0) {
			return;
		}
	}

	if (fileName.indexOf('$') >= 0 || fileName.indexOf('.~') >= 0 || fileName.indexOf('~') >= 0 || fileName.indexOf('.json') >= 0 || fileName.indexOf('.bat') >= 0) {
		return;
	}
	console.log("parseFile  fileName ", fileName)
	//忽略文件列表
	for (var i = 0; i < configObj.ignoreFileList.length; i++) {
		if (fileName.indexOf(configObj.ignoreFileList[i]) >= 0) {
			return;
		}
	}
	let excel = new ExportExcel(xlsx.parse(fileName), exportPath, configObj)
	excel.export()
	excelList.push(excel)

}


function readDir(dir, exportPath) {

	// console.log(' readDir ===================')
	var stat = fs.statSync(dir);
	if (!stat.isDirectory()) {
		return;
	}
	var subpaths = fs.readdirSync(dir),
		subpath;
	for (var i = 0; i < subpaths.length; ++i) {
		if (subpaths[i][0] === '.') {
			continue;
		}
		subpath = path.join(dir, subpaths[i]);
		// console.log(' readDir   subpath ', subpath)
		stat = fs.statSync(subpath);
		if (stat.isDirectory()) {
			readDir(subpath, exportPath);
		} else if (stat.isFile()) {
			parseFile(subpath, exportPath);
		}
	}
}

function init(cur_path, config_path) {
	var config = null;
	if (config_path) {
		config = file_util.readFile(config_path)
	} else {
		config = file_util.readFile(path.join(cur_path, 'config.json'))
	}
	configObj = JSON.parse(config)
	for (let index = 0; index < configObj.copy_info.length; index++) {
		let tsExport = exports[index]
		if (tsExport) {
			tsExport.init(cur_path, configObj.copy_info[index])
		}
	}
	console.log(" configObj ", configObj)
}
var test = function () {
	// Editor.log(" test ")
}

function exportDefine(export_dir) {
	let lang = new ExportLang()
	lang.export(export_dir, excelList)

	let exportIndex = configObj.exportIndex;
	for (let index = 0; index < exportIndex.length; index++) {
		const element = exportIndex[index];
		if (element) {
			let tsExport = exports[index]
			tsExport.export(export_dir, excelList)
		}
	}


}

function copy() {
	let exportIndex = configObj.exportIndex;
	for (let index = 0; index < exportIndex.length; index++) {
		const element = exportIndex[index];
		if (element) {
			let tsExport = exports[index]
			tsExport.copy(configObj.moduleName, configObj.export_path)
		}
	}
}

function exportAll() {
	let data_path = configObj.data_path;
	let export_path = configObj.export_path;
	var json_path = path.join(export_path, Define.CLIENT_FOLDER)
	//创建目录
	if (configObj.isClear) {
		file_util.delDir(export_path);
	}
	file_util.mkdir(export_path);
	file_util.mkdir(json_path);
	readDir(data_path, export_path);
	exportDefine(export_path)
	copy()
}

if (configObj && configObj.data_path && configObj.export_path) {
	exportAll();
}

module.exports.exportAll = exportAll
module.exports.init = init;
